<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- STYLESHEETS -->
    <!--[if lt IE 9]><script src="../../js/flot/excanvas.min.js"></script><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->
    <link rel="stylesheet" type="text/css" href="../../css/cloud-admin.css">
    <link rel="stylesheet" type="text/css" href="../../css/themes/default.css" id="skin-switcher">
    <link rel="stylesheet" type="text/css" href="../../css/responsive.css">
    <link href="../../font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!-- file-input -->
    <link rel="stylesheet" type="text/css" href="../../js/file-input/fileinput.css" />

    <!-- datatable -->
    <link rel="stylesheet" href="../../js/datatables/datatable.css">
    <link rel="stylesheet" type="text/css" href="../../js/select2/select2.min.css" />
    <style>
        .red {
            color: red;
            font-size: 12px;
            display: flex;
            align-items: center;
            height: 34px;
        }
    </style>
</head>

<body>
    <header class="navbar clearfix" id="header">
    </header>
    <!-- PAGE -->
    <section id="page">
        <!-- SIDEBAR -->
        <div id="sidebar" class="sidebar">
            <div class="sidebar-menu nav-collapse">

                <!-- SIDEBAR MENU -->
                <ul>

                </ul>
                <!-- /SIDEBAR MENU -->
            </div>

        </div>
        <!-- /SIDEBAR -->
        <div id="main-content">
            <!-- SAMPLE BOX CONFIGURATION MODAL FORM-->
            <div class="modal fade" id="box-config" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title">维护批发商</h4>
                        </div>
                        <div class="modal-body">
                            <div class="row">
                                <label class="control-label col-md-3" style="line-height:34px;"><span class="required">*</span>批发商名称：</label>
                                <div class="col-md-4">
                                    <input type="text" class="form-control" name="supplierName" placeholder="请输入批发商名称" maxlength="20" />
                                    <input type="hidden" name="id" />
                                </div>
                                <div class="col-md-5 red">名称不能重复</div>
                            </div>
                            <div class="row">
                                <label class="control-label col-md-3" style="line-height:34px;"><span class="required">*</span>店铺编号：</label>
                                <div class="col-md-4">
                                    <input type="text" class="form-control" name="supplierId" placeholder="请输入店铺编号" maxlength="20" />

                                </div>
                                <div class="col-md-5 red">请到旺店通 设置->基础设置->店铺页面查看，保存后不可修改</div>
                            </div>
                            <div class="row" style="margin-bottom:10px">
                                <label class="control-label col-md-3" style="line-height:34px;"><span class="required">*</span>所属分类：</label>
                                <div class="col-md-4">
                                    <select id="type_select" name="typeSelect" class="col-md-12 select2-offscreen" tabindex="-1">
                                        <option></option>
                                        <option value='PaymentFirst'>先款后货</option>
                                        <option value='ShipmentFirst'>先货后款</option>
                                    </select>
                                </div>
                            </div>
                            <div class="row">
                                <label class="control-label col-md-3" style="line-height:34px;"><span class="required">*</span>结算周期：</label>
                                <div class="col-md-4">
                                    <input type="text" class="form-control" name="settlementTime" placeholder="请输入结算周期" maxlength="20" />

                                </div>
                                <div class="col-md-5 red">不能输入大于30的数字，30代表按自然月生成结算账单</div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                            <button type="button" class="btn btn-success bt-save">保存</button>
                        </div>
                    </div>
                </div>
            </div>

            <div class="modal fade" id="recharge-config" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                            <h4 class="modal-title">提示：请确认财务已经收到钱</h4>
                        </div>
                        <div class="modal-body">
                            <div class="row">
                                <label class="control-label col-md-3" style="line-height:34px;"><span class="required">*</span>充值金额：</label>
                                <div class="col-md-4">
                                    <input type="text" class="form-control" name="rechargeMoney" placeholder="请输入充值金额" maxlength="20" />
                                    <input type="hidden" name="wholesalerId" />
                                </div>
                            </div>
                            <div class="row">
                                <label class="control-label col-md-3" style="line-height:34px;">备注：</label>
                                <div class="col-md-4">
                                    <input type="text" class="form-control" name="remark" placeholder="请输入备注" maxlength="20" />
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                            <button type="button" class="btn btn-success bt-recharge-save">保存</button>
                        </div>
                    </div>
                </div>
            </div>

            <!-- /SAMPLE BOX CONFIGURATION MODAL FORM-->
            <div class="container">
                <div class="row">
                    <div id="content" class="col-lg-12">
                        <!-- PAGE HEADER-->
                        <div class="row" style="height: 30px;">
                            <div class="col-sm-12">
                                <div class="page-header">
                                    <!-- STYLER -->

                                    <!-- /STYLER -->
                                    <!-- BREADCRUMBS -->
                                    <ul class="breadcrumb">
                                        <li>
                                            <i class="fa fa-home"></i>
                                            <a href="../index.html">首页</a>
                                        </li>
                                        <li>
                                            <a href="#">商品管理</a>
                                        </li>
                                        <li>商品批发商</li>
                                    </ul>
                                    <!-- /BREADCRUMBS -->

                                </div>
                            </div>
                        </div>
                        <!-- /PAGE HEADER -->
                        <!-- FORMS QUERY-->
                        <div class="row">
                            <div class="col-md-12">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="box border primary">
                                            <div class="box-title">
                                                <h4><i class="fa fa-bars"></i>查询条件</h4>
                                            </div>
                                            <div class="box-body big" style="padding:10px;">
                                                <form class="form-horizontal" role="form">
                                                    <div class="form-group mrg-bt10">
                                                        <label class="col-sm-1 control-label" style="width: auto">批发商名称：</label>
                                                        <div class="col-sm-2">
                                                            <input type="text" class="form-control" name="q" placeholder="输入关键字">
                                                        </div>
                                                        <div class="col-sm-1">
                                                            <a class="btn btn-primary search">查询</a>
                                                        </div>
                                                    </div>
                                                </form>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <!-- /FORMS QUERY -->
                        <div class="row" style="height: 30px;">
                            <div class="col-md-12">
                                <div class="box ">
                                    <a id="create" data-toggle="modal" class="btn btn-success config">
                                        <i class="fa  fa-plus-square-o"></i> 新增批发商
                                    </a>

                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <!-- TABLES -->
                            <div class="col-md-12">
                                <!-- BOX -->
                                <div class="box border primary">
                                    <div class="box-title">
                                        <h4><i class="fa fa-table"></i>批发商数：<span id="total"></span>条 </h4>

                                    </div>
                                    <div class="box-body">
                                        <table id="db_brand_list" cellpadding="0" cellspacing="0" border="0" class="datatable table table-striped table-bordered table-hover">

                                        </table>
                                    </div>
                                </div>
                                <!-- /BOX -->
                            </div>
                        </div>
                        <!-- /TABLES -->

                        <div class="footer-tools">
                            <span class="go-top">
								<i class="fa fa-chevron-up"></i> Top
							</span>
                        </div>
                    </div>
                    <!-- /CONTENT-->
                </div>
            </div>
        </div>
        <input type="hidden" name="brandId" />
    </section>

    <!--/PAGE -->
    <!-- JAVASCRIPTS -->
    <!-- Placed at the end of the document so the pages load faster -->
    <!-- JQUERY -->
    <script src="../../js/jquery/jquery-2.0.3.min.js"></script>
    <!-- JQUERY UI-->
    <script src="../../js/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.min.js"></script>
    <!-- BOOTSTRAP -->
    <script src="../../bootstrap-dist/js/bootstrap.min.js"></script>

    <!-- BLOCK UI -->
    <script type="text/javascript" src="../../js/jQuery-BlockUI/jquery.blockUI.min.js"></script>

    <!-- AUTOSIZE -->
    <script type="text/javascript" src="../../js/autosize/jquery.autosize.min.js"></script>

    <!-- INPUT MASK -->
    <script type="text/javascript" src="../../js/bootstrap-inputmask/bootstrap-inputmask.min.js"></script>
    <!-- SELECT2 -->
    <script type="text/javascript" src="../../js/select2/select2.min.js"></script>

    <!-- bootstrap-fileupload -->
    <script type="text/javascript" src="../../js/jquery-upload/js/jquery.iframe-transport.js"></script>
    <script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload.min.js"></script>
    <script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload-process.min.js"></script>
    <script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload-validate.min.js"></script>

    <!-- UNIFORM -->
    <script type="text/javascript" src="../../js/uniform/jquery.uniform.min.js"></script>
    <!-- spinner -->
    <script src="../../js/spinner/spin.js"></script>
    <!-- DATA TABLES -->
    <script type="text/javascript" src="../../js/datatables/bootstrap-select.js"></script>
    <script type="text/javascript" src="../../js/datatables/datatable.js"></script>
    <script type="text/javascript" src="../../js/datatables/datatable-bootstrap.js"></script>
    <script type="text/javascript" src="../../js/datatables/datatable-tabletools.js"></script>
    <script type="text/javascript" src="../../js/datatables/fnAddTr.js"></script>

    <!--bootbox-->
    <script type="text/javascript" src="../../js/bootbox/bootbox.min.js"></script>
    <!-- COOKIE -->
    <script type="text/javascript" src="../../js/jQuery-Cookie/jquery.cookie.min.js"></script>
    <!-- CUSTOM SCRIPT -->
    <script src="../../js/script.js"></script>
    <script src="../../js/common.js"></script>
    <script>
        var Brand = function() {
            //保存编辑
            var saveSupplier = function() {

                $(".form-group").removeClass("has-error");
                var wholesalerName = $("input[name=supplierName]").val();
                var shopNo = $("input[name=supplierId]").val();
                var billingType = $('#type_select').val();
                var billingCycle = $("input[name=settlementTime]").val();
                console.log(billingCycle);

                if (wholesalerName == "") {
                    $("input[name=supplierName]").parents(".row").addClass("has-error");
                    return false;
                }
                if (shopNo == "") {
                    $("input[name=supplierId]").parents(".row").addClass("has-error");
                    return false;
                }
                if (billingType == "") {
                    $('#type_select').parents(".row").addClass("has-error");
                    return false;
                }
                if (billingCycle == "") {
                    $("input[name=settlementTime]").parents(".row").addClass("has-error");
                    return false;
                }

                var supplier = {};
                supplier.id = $("input[name=id]").val();
                supplier.wholesalerName = wholesalerName;
                supplier.shopNo = shopNo;
                supplier.billingType = billingType;
                supplier.billingCycle = billingCycle
                    // return
                $.ajax({
                    type: "post",
                    url: App.getContextPath() + "admin/wholesaler/edit.do",
                    async: true,
                    data: supplier,
                    success: function(result) {
                        if (result.type === 'error') {
                            $.alert(result.content)
                            return
                        }
                        console.log(result);
                        $("#box-config").modal("hide");
                        $.clearInput();
                        searchSupplier();
                    }
                });


            };
            var saveRecharge = function() {
                $(".form-group").removeClass("has-error");
                var rechargePrice = $("input[name=rechargeMoney]").val();
                var remark = $("input[name=remark]").val();

                if (rechargePrice == "") {
                    $("input[name=rechargeMoney]").parents(".row").addClass("has-error");
                    return false;
                }


                var rechargeObj = {};
                rechargeObj.wholesalerId = $("input[name=wholesalerId]").val();
                rechargeObj.rechargePrice = rechargePrice;
                rechargeObj.remark = remark;
                // return
                $.ajax({
                    type: "post",
                    url: App.getContextPath() + "admin/wholesaler/recharge.do",
                    async: true,
                    data: rechargeObj,
                    success: function(result) {
                        if (result.type === 'error') {
                            $.alert(result.content)
                            return
                        }
                        $("#recharge-config").modal("hide");
                        $.clearInput();
                        searchSupplier();
                    }
                });


            };

            var searchSupplier = function() {
                var url = App.getContextPath() + "admin/wholesaler/search.do?wholesalerName=" + $("input[name=q]").val();
                var dtSetting = $.dataTableSetting({
                    "ajaxSource": url,
                    "serverSide": true,
                    "bFilter": false, // 去掉搜索框
                    "bLengthChange": true, // 每页显示数量
                    "enableCheckbox": true,
                    "checkboxColIndex": 0,
                    "sScrollX": "100%",
                    "sScrollXInner": "100%",
                    "iDisplayLength": 10,

                    "bSort": false,
                    "aoColumns": [{
                        "mDataProp": "id",
                        "sTitle": "批发商ID",
                        "width": "10%"
                    }, {
                        "mDataProp": "wholesalerName",
                        "sTitle": "批发商名称",
                        "width": "10%"
                    }, {
                        "mDataProp": "shopNo",
                        "sTitle": "店铺编号",
                        "width": "10%"
                    }, {
                        "mDataProp": "billingTypeDesc",
                        "sTitle": "结算类型 ",
                        "width": "8%"

                    }, {
                        "mDataProp": "billingType",
                        "sTitle": "结算类型 ",
                        "class": "hidden",
                        "width": "0"
                    }, {
                        "mDataProp": "billingCycle",
                        "sTitle": "结算周期",
                        "class": "hidden",
                        "width": "0"
                    }, {
                        // "mDataProp": "totalPrepaidPrice",
                        "sTitle": "总预付款",
                        "width": "8%",
                        "render": function(data, type, row) {
                            return "<a target='_blank' href='./recharge_log.html?id=" + row.id + "'>" + format_thousands(row.totalPrepaidPrice) + "</a>"
                        }
                    }, {
                        // "mDataProp": "amountDuePrice",
                        "sTitle": "应结金额",
                        "width": "10%",
                        "render": function(data, type, row) {
                            return "<a target='_blank' href='./presettlement_list.html?id=" + row.id + "'>" + format_thousands(row.amountDuePrice) + "</a>"
                        }
                    }, {
                        // "mDataProp": "amountPrice",
                        "sTitle": "已结金额",
                        "width": "7%",
                        "render": function(data, type, row) {
                            return "<a target='_blank' href='./bill_list.html?wholesalerName=" + row.wholesalerName + "'>" + format_thousands(row.amountPrice) + "</a>"
                        }
                    }, {
                        // "mDataProp": "overPrice",
                        "sTitle": "余额",
                        "width": "7%",
                        "render": function(data, type, row) {
                            return format_thousands(row.overPrice);
                        }
                    }, {
                        "sTitle": "操作",
                        "render": function(data, type, row) {
                            var str = "<p class='btn-toolbar' style='margin:0px'>" +
                                "<button class='btn btn-success btn-xs bt_edit btn-width'><i class='fa fa-pencil-square-o'></i>编辑</button>" +
                                "<button class='btn btn-danger btn-xs bt_recharge btn-width'><i class='fa fa-music'></i> 充值</button>" + "<a target='_blank' href='./trade_price_edit.html?id=" + row.id + "' style='margin-left:5px'><button class='btn btn-primary btn-xs btn-width'><i class='fa fa-money'></i> 批发价</button></p></a>";
                            return str;
                        },
                        "width": "15%"
                    }]
                });

                $mainDataTable = $("#db_brand_list").dataTable(dtSetting);
            };
            var showDialog = function() {
                // fileUpLoad();
                $.clearInput();
                $("#box-config").modal("show");
            };

            function showRechageDialog() {
                // fileUpLoad();
                $("#recharge-config input").val('')
                $("#recharge-config").modal("show");
            };

            var fileUpLoad = function() {
                $("[upload-file=imageUrlProduct]").fileupload({
                    url: App.getContextPath() + "admin/file/uploadImage.do",
                    formData: {
                        fileType: "ProductDetailImage"
                    },
                    dataType: 'json',
                    acceptFileTypes: /(\.|\/)(gif|jpe?g|png|JPG?g)$/i,
                    iframe: true,
                    autoUpload: true,
                    done: function(e, data) {

                        var result = data.result;
                        if (result.message != "") {
                            $.alert(result.message);
                            return false;
                        } else {
                            var $tr = $(this).closest("form");
                            $tr.find("[hidden-image]").append("<input type='hidden'  value='" + result.fileUrl + "' />");
                            $tr.find(".image_preview_default").html("<img src='" + result.fileUrl + "' />");
                        }
                    }
                });

            };

            function recharge() {
                showRechageDialog()
                var tr = $(this).closest("tr");
                $("#recharge-config").find("input[name=wholesalerId]").val($(tr).find("td:eq(0)").html());
                // if ($(tr).find(".productNum").html() != "0") {
                //     $.alert("批发商下已经绑定商品，请先解除关系后，再删除！");
                //     return;
                // } else {

                // }
            };
            return {

                init: function() {
                    searchSupplier();
                    $(".search").click(searchSupplier);
                    $(".bt-save").click(saveSupplier);
                    $(".bt-recharge-save").click(saveRecharge);
                    $("#type_select").select2();

                    $("#db_brand_list").on("click", ".bt_edit", function() {
                        var typeSelect = $(this).attr("typeSelect");
                        showDialog();
                        var tr = $(this).closest("tr");
                        $("#type_select").select2("val", $(tr).find("td:eq(4)").html());
                        $("#box-config").find("input[name=id]").val($(tr).find("td:eq(0)").html());
                        $("#box-config").find("input[name=supplierName]").val($(tr).find("td:eq(1)").html());
                        $("#box-config").find("input[name=supplierId]").val($(tr).find("td:eq(2)").html());
                        $("#box-config").find("input[name=settlementTime]").val($(tr).find("td:eq(5)").html());
                        $("#box-config").find("[name=brandDesc]").val($(tr).find(".brandDesc").html());
                        $("#type_select").val($(tr).find("td:eq(4)").html());
                        console.log($(tr).find("td:eq(4)").html());
                        $(".image_preview_default:eq(0)").html("<img src='" + $(tr).find("td:eq(2)").find("img").attr("src") + "'>");
                        $("[hidden-image]:eq(0)").append("<input type='hidden' value='" + $(tr).find("td:eq(2)").find("img").attr("src") + "' name='logo'>");

                        $(".image_preview_default:eq(1)").html("<img src='" + $(tr).find(".bannerImagePath").find("img").attr("src") + "'>");
                        $("[hidden-image]:eq(1)").append("<input type='hidden' value='" + $(tr).find(".bannerImagePath").find("img").attr("src") + "' >");

                        $("#box-config").modal("show");
                    });

                    $("#db_brand_list").on("click", ".bt_recharge", recharge);

                    $("#create").click(showDialog);

                }
            }
        }();


        $(function() {
            $("#header").load("../head.html");
            App.init(); //Initialise plugins and elements
            Brand.init();

        });
    </script>
    <!-- /JAVASCRIPTS -->
</body>

</html>